<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/23
 * Time: 10:39
 * 总部
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\AgentSendTips;
use app\common\model\AgentShipment;
use app\common\model\AgentShop;
use app\common\model\Commission;
use app\common\service\UserService;
use think\Db;

class Center extends Api {
	
	/**
	 * 统计
	 */
	public function statistics() {
		$userId = $this->auth->id;
		$level = $this->auth->level;
		$isAdmin = false;
		if($level != \app\admin\model\User::LEVEL_CENTER) {
			$adminLevel = \app\common\model\AgentAdmin::getAdminLevel($userId);
			if(!$adminLevel || $adminLevel['role'] != \app\admin\model\User::LEVEL_CENTER) {
				$this->error('权限不足');
			}
			$isAdmin = true;
			$userId = $adminLevel['user_id'];
		}
		
		$agentInfo = Db::name('agentShop')
			->where(['user_id' => $userId, 'status' => AgentShop::STATUS_Y])
			->field('name, image, sale_number, order_number, sale_amount, id, status, is_group')
			->find();
		if(!$agentInfo || $agentInfo['status'] == AgentShop::STATUS_D) {
			$this->error('当前身份已被禁用');
		}
		//场景店
		$shop = Db::name('agentShop')
			->where([
				'role' => \app\admin\model\User::LEVEL_SHOP,
				'center_id' => $agentInfo['id'],
				'status' => AgentShop::STATUS_Y
			])
			->count();
		
		//提货点
		$warehouse = Db::name('agentShop')
			->where([
				'role' => \app\admin\model\User::LEVEL_WAREHOUSE,
				'center_id' => $agentInfo['id'],
				'status' => AgentShop::STATUS_Y
				])
			->count();
		
		//推广员
		$agents = Db::name('agentShop')
			->where(['center_id' => $agentInfo['id'], 'status' => AgentShop::STATUS_Y])
			->column('user_id', 'id');
		$ids = array_values($agents);
		$ids[] = $userId;
		$promotion = Db::name('promotion')
			->where(['recommend_user_id' => ['in', $ids], 'status' => \app\common\model\Promotion::STATUS_Y])
			->count();
		
		/**
		 * 待入库订单
		 */
		$orderNumber = Db::name('agentShipment')
			->where(['sign_agent_id' => $agentInfo['id'], 'status' => AgentShipment::STATUS_WAIT])
			->count();
		
		//我的奖励数量
		$user = $this->auth->getUser();
		$selfNumber = $user['reward_number'];
		
		//我场景店的奖励数量
		$rewardNumber = Db::name('agentShop')->alias('a')
			->join('user u', 'a.user_id = u.id', 'left')
			->where(['a.role' => \app\admin\model\User::LEVEL_SHOP, 'a.recommend_user_id' => $userId, 'a.status' => AgentShop::STATUS_Y])
			->sum('u.reward_number');
		
		
		
		//粉丝
		if($this->area == \app\admin\model\User::AREA_OTHER) {
			$fans = UserService::getFansNumById($userId);
		} else {
			//深圳的粉丝是自己的粉丝，加下下级场景店的粉丝
			$sonShop =Db::name('agentShop')
				->where(['role' => \app\admin\model\User::LEVEL_SHOP, 'recommend_user_id' => $userId, 'status' => AgentShop::STATUS_Y])
				->column('user_id', 'id');
			$userIds = array_merge($sonShop, [$userId]);
			$fans = Db::name('user')
				->where(['recommend_user_id' => ['in', $userIds]])
				->count();
		}
		
		//未发货提醒数量
		$sendNumber = Db::name('agentSendTips')->where(['user_id' => $this->auth->id, 'status' => AgentSendTips::STATUS_WAIT])
			->count();
		
		//销售佣金
		$saleCommission = Db::name('commission')->where(['user_id' => $userId, 'type' => ['in', [Commission::TYPE_SALE, Commission::TYPE_RECOMMEND]]])->sum('amount');
		
		//提货佣金
		$goodsCommission = Db::name('commission')->where(['user_id' => $userId, 'type' => Commission::TYPE_GOODS])->sum('amount');
		
		$stockCommission = Db::name('commission')->where(['user_id' => $userId, 'type' => Commission::TYPE_STOCK])->sum('amount');
		
		$this->success('查询成功', [
			'fans' => $fans,
			'order_number' => $agentInfo['order_number'],
			'sale_number' => $agentInfo['sale_number'],
			'order_amount' => $agentInfo['sale_amount'],
			'shop' => $shop,
			'warehouse' => $warehouse,
			'stock_order_number' => $orderNumber,
			'promotion' => $promotion,
			'self_number' => $selfNumber,
			'reward_number' => $rewardNumber,
			'is_group' => $agentInfo['is_group'],
			'agent_info' => $agentInfo,
			'is_admin' => $isAdmin,
			'send_number' => $sendNumber,
			'user_id' => $userId,
			'sale_commission' => $saleCommission,
			'goods_commission' => $goodsCommission,
			'stock_commission' => $stockCommission,
			
		]);
	}
}
